import { Node, Edge } from 'reactflow';

export interface WorkflowNodeData {
  label: string;
  config?: {
    fields: Array<{
      name: string;
      label: string;
      type: string;
      default?: any;
      options?: Array<{ label: string; value: string }>;
    }>;
  };
}

export interface WorkflowNode extends Omit<Node, 'data'> {
  id: string;
  type: string;
  data: WorkflowNodeData;
  position: { x: number; y: number };
}

export interface WorkflowEdge extends Omit<Edge, 'id' | 'source' | 'target'> {
  id: string;
  source: string;
  target: string;
  label?: string;
}

export interface WorkflowTemplate {
  id: string;
  name: string;
  description: string;
  nodes: WorkflowNode[];
  edges: WorkflowEdge[];
}

// 标准开发工作流
const standardWorkflow: WorkflowTemplate = {
  id: 'standard',
  name: '标准开发工作流',
  description: '标准的开发、测试和部署流程',
  nodes: [
    // 开发环境
    {
      id: 'dev-container',
      type: 'development',
      data: {
        label: '开发容器',
        config: {
          fields: [
            { name: 'buildCommand', label: '构建命令', type: 'input', default: 'cargo build --release' },
            { name: 'buildLocation', label: '构建位置', type: 'select', options: [
              { label: 'Dev容器构建', value: 'dev' },
              { label: 'Test容器构建', value: 'test' }
            ]}
          ]
        }
      },
      position: { x: 100, y: 100 }
    },
    // 测试环境
    {
      id: 'test-container',
      type: 'testing',
      data: {
        label: '测试容器',
        config: {
          fields: [
            { name: 'testCommand', label: '测试命令', type: 'input', default: 'cargo test' },
            { name: 'testServer', label: '测试服务器', type: 'select' },
            { name: 'triggerType', label: '触发方式', type: 'select', options: [
              { label: '代码提交触发', value: 'commit' },
              { label: '手动触发', value: 'manual' }
            ]}
          ]
        }
      },
      position: { x: 300, y: 100 }
    },
    // 生产环境
    {
      id: 'prod-build',
      type: 'build',
      data: {
        label: '生产构建',
        config: {
          fields: [
            { name: 'buildType', label: '构建类型', type: 'select', options: [
              { label: '生产容器镜像', value: 'container' },
              { label: '目标平台二进制', value: 'binary' }
            ]},
            { name: 'optimization', label: '优化级别', type: 'select', options: [
              { label: 'O2', value: '2' },
              { label: 'O3', value: '3' }
            ]}
          ]
        }
      },
      position: { x: 500, y: 100 }
    },
    {
      id: 'deploy-options',
      type: 'deployment',
      data: {
        label: '部署选项',
        config: {
          fields: [
            { name: 'deployTarget', label: '部署目标', type: 'select', options: [
              { label: '生产服务器', value: 'server' },
              { label: 'GitHub', value: 'github' },
              { label: '阿里云', value: 'aliyun' }
            ]},
            { name: 'updateStrategy', label: '更新策略', type: 'select', options: [
              { label: '自动更新', value: 'auto' },
              { label: '手动更新', value: 'manual' },
              { label: '定时更新', value: 'scheduled' }
            ]}
          ]
        }
      },
      position: { x: 500, y: 250 }
    },
    {
      id: 'prod-server',
      type: 'server',
      data: {
        label: '生产服务器'
      },
      position: { x: 700, y: 100 }
    },
    {
      id: 'github',
      type: 'cloud',
      data: {
        label: 'GitHub'
      },
      position: { x: 700, y: 250 }
    },
    {
      id: 'aliyun',
      type: 'cloud',
      data: {
        label: '阿里云'
      },
      position: { x: 700, y: 400 }
    },
    {
      id: 'monitoring',
      type: 'monitoring',
      data: {
        label: '监控系统',
        config: {
          fields: [
            { name: 'healthCheck', label: '健康检查', type: 'switch' },
            { name: 'performanceMonitor', label: '性能监控', type: 'switch' },
            { name: 'resourceMonitor', label: '资源监控', type: 'switch' },
            { name: 'logCollection', label: '日志收集', type: 'switch' }
          ]
        }
      },
      position: { x: 900, y: 100 }
    }
  ],
  edges: [
    // 开发到测试
    {
      id: 'dev-to-test',
      source: 'dev-container',
      target: 'test-container',
      label: '提交测试'
    },
    // 测试到生产构建
    {
      id: 'test-to-prod',
      source: 'test-container',
      target: 'prod-build',
      label: '测试通过'
    },
    // 生产构建到部署选项
    {
      id: 'build-to-deploy',
      source: 'prod-build',
      target: 'deploy-options',
      label: '选择部署方式'
    },
    // 部署选项到各目标
    {
      id: 'deploy-to-server',
      source: 'deploy-options',
      target: 'prod-server',
      label: '部署到生产'
    },
    {
      id: 'deploy-to-github',
      source: 'deploy-options',
      target: 'github',
      label: '推送到GitHub'
    },
    {
      id: 'deploy-to-aliyun',
      source: 'deploy-options',
      target: 'aliyun',
      label: '推送到阿里云'
    },
    // 生产服务器到监控
    {
      id: 'server-to-monitoring',
      source: 'prod-server',
      target: 'monitoring',
      label: '监控'
    }
  ]
};

// 简化开发工作流
const simpleWorkflow: WorkflowTemplate = {
  id: 'simple',
  name: '简化开发工作流',
  description: '适用于小型项目的简化开发流程',
  nodes: [
    {
      id: 'dev',
      type: 'development',
      data: {
        label: '开发环境'
      },
      position: { x: 100, y: 100 }
    },
    {
      id: 'prod',
      type: 'server',
      data: {
        label: '生产环境'
      },
      position: { x: 300, y: 100 }
    }
  ],
  edges: [
    {
      id: 'dev-to-prod',
      source: 'dev',
      target: 'prod',
      label: '直接部署'
    }
  ]
};

export const workflowTemplates = [
  standardWorkflow,
  simpleWorkflow
]; 